package bupt.party.online.service.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import bupt.party.online.dao.IPartyNewsDao;
import bupt.party.online.model.Branch;
import bupt.party.online.model.PartyNews;
import bupt.party.online.service.IPartyNewsService;

public class PartyNewsServiceImpl implements IPartyNewsService {
	private IPartyNewsDao partyNewsDao;
	
	public List<PartyNews> getSpiritNews(int pageNow,int pageSize) {
		// TODO Auto-generated method stub
		int firstResult = (pageNow-1)*pageSize;
		return this.partyNewsDao.getResultsByPage(1, firstResult, pageSize);
	}

	public IPartyNewsDao getPartyNewsDao() {
		return partyNewsDao;
	}

	public void setPartyNewsDao(IPartyNewsDao partyNewsDao) {
		this.partyNewsDao = partyNewsDao;
	}

	public int getResultCountByTag(int tag) {
		// TODO Auto-generated method stub
		
		if(tag <= 0) return 0;
		
		else return this.partyNewsDao.getResultCountByTag(tag);
	}

	public List<PartyNews> getNewsByCategory(int cateTag, int pageNow,
			int pageSize) {
		// get news list according to category of news
		
		if(cateTag <= 0) return null;
		
		int firstResult = pageSize*(pageNow - 1);
		
		return partyNewsDao.getResultsByPage(cateTag, firstResult, pageSize);
		
	}

	public PartyNews getNewsById(int id) {
		// TODO Auto-generated method stub
		if(id < 0) id = 0;
		String sql = "from PartyNews p where p.id="+id;
		
		List<PartyNews> newsList = partyNewsDao.findBySql(sql);
		
		if(newsList != null && newsList.size() > 0) return newsList.get(0);
		
		return null;
	}

	public List<PartyNews> getNewsByConditions(Map<String,Object> conditions,int pageNow,int pageSize) {
		// TODO Auto-generated method stub
		
		String queryString = "from PartyNews ";
		
		int firstResult = pageSize*(pageNow -1);
		
		if(firstResult < 0) firstResult = 0;
		
		if(conditions == null || conditions.size() <= 0) return this.partyNewsDao.QueryByConditions(queryString,null,firstResult,pageSize);
		
		Object[] queryValues = new Object[conditions.size()];
		
		Iterator it = conditions.entrySet().iterator();
		
		int i=0;
		
		while(it.hasNext()){
			
			Map.Entry<String, Object> entry = (Entry<String, Object>) it.next();
			
			queryValues[i] = entry.getValue();
			
			if(0==i){
				
				if(entry.getKey().equals("title")) queryString += " where "+entry.getKey()+" like ? ";
				
				else queryString += " where "+entry.getKey()+"=? ";
				
				i++;
				
				continue;
			} 
			
			if(entry.getKey().equals("title")) queryString +=" and "+entry.getKey()+" like ? ";
			
			else queryString +=" and "+entry.getKey()+"=? ";
			
			i++;
			
		}
		
		return this.partyNewsDao.QueryByConditions(queryString,queryValues,firstResult,pageSize);
	}

	@Override
	public int getResultCountByConditions(Map<String, Object> conditions) {
		// TODO Auto-generated method stub
		
		String countSql = "select count(*) from PartyNews ";
		
		if(conditions == null || conditions.size() <= 0) return this.partyNewsDao.getResultCount(countSql,null);
		
		Object[] queryValues = new Object[conditions.size()];
		
		Iterator it = conditions.entrySet().iterator();
		
		int i=0;
		
		while(it.hasNext()){
			
			Entry entry = (Entry) it.next();
			
			queryValues[i] = entry.getValue();
			
			if(0==i){
				
				if(entry.getKey().equals("title")) countSql += " where "+entry.getKey()+" like ? ";
				
				else countSql += " where "+entry.getKey()+"=? ";
				
				i++;
				
				continue;
			} 
			
			if(entry.getKey().equals("title")) countSql +=" and "+entry.getKey()+" like ? ";
				
			else countSql +=" and "+entry.getKey()+"=? ";
			
			i++;
			
		}
		
		return this.partyNewsDao.getResultCount(countSql,queryValues);
	}

	@Override
	public boolean saveOrUpdateSpiritNews(PartyNews news) {
		// TODO Auto-generated method stub
		try{
			
			this.partyNewsDao.saveOrUpdate(news);
			return true;
			
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean deleteNewsById(int id) {
		// TODO Auto-generated method stub
		
		if(id < 0) return false;
		
		try{
			return this.partyNewsDao.deleteNewsById(id);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public List<PartyNews> getLatest4PictureNews() {
		// TODO Auto-generated method stub
		
		String sql = "from PartyNews p where p.picTag=? order by postTime desc";
		
		List<PartyNews> results = partyNewsDao.QueryByConditions(sql, new Object[]{1}, 0, 4);
		
		return results;
	}

	@Override
	public List<PartyNews> getCollegeNewsByBranch(List<Branch> branches,
			String queryString, int firstResult, int maxResults) {
		// TODO Auto-generated method stub
		
		Set<Integer> branchIds = new HashSet<Integer>();
		
		for(Branch b : branches){
			branchIds.add(b.getId());
		}
		
		return this.partyNewsDao.findCollegeNews(queryString, branchIds, firstResult, maxResults);
	}

	@Override
	public int getCollegeNewsCount(List<Branch> branches, String queryString) {
		// TODO Auto-generated method stub
		
		Set<Integer> branchIds = new HashSet<Integer>();
		
		for(Branch b : branches){
			branchIds.add(b.getId());
		}
		
		return this.partyNewsDao.getCollegeNewsCount(queryString, branchIds);
	
	}

}
